package elm.example.controller;

import elm.example.dao.IBusinessDao;
import elm.example.po.Business;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/*
    classDescription:商家信息控制类，实现商家信息的相关服务逻辑
*/

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/business")
@Tag(name = "商家信息相关服务")
public class BusinessController {

   @Resource
   private IBusinessDao businessDao;

   @Operation(summary = "获取所有商家信息")
   @GetMapping(value = "/findAll")
   public List<Business> findAllBusiness() {
      return StreamSupport.stream(businessDao.findAll().spliterator(),false)
              .collect(Collectors.toList());
   }

   @Operation(summary = "新增商家")
   @Parameter(name="business",description = "商家信息JSON格式（不需要手动设置id）")
   @PostMapping(value = "/createBusiness")
   public Business insert(@RequestBody Business business) {
      return businessDao.save(business);
   }

   @Operation(summary = "修改商家信息")
   @Parameter(name="business",description = "用户信息JSON格式")
   @PutMapping(value = "/updateBusiness")
   public Boolean update(@RequestBody Business business) {
      businessDao.save(business);
      return true;
   }

   @Operation(summary = "删除商家")
   @Parameter(name="id",description = "商家id")
   @DeleteMapping(value = "/deleteBusiness")
   public Boolean delete(@RequestParam(value = "id")Integer businessId) {
      businessDao.deleteById(Integer.valueOf(businessId));
      return true;
   }

}
